package cn.dujc.superkid.fragment.main;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.dujc.core.ui.BaseFragment;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.activities.CourseSelectionDetailActivity;
import cn.dujc.superkid.activities.MatchDetailActivity;
import cn.dujc.superkid.activities.OnlineAnswerActivity;
import cn.dujc.superkid.activities.SearchActivity;
import cn.dujc.superkid.adapter.CourseFragmentLeftAdapter;
import cn.dujc.superkid.adapter.CourseFragmentRightAdapter;
import cn.dujc.superkid.adapter.MatchFragmentLeftAdapter;
import cn.dujc.superkid.adapter.MatchFragmentRightAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.bean.CupIndexResults;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.bean.VideoIndexResults;
import cn.dujc.superkid.fragment.UtilFragment;
import cn.dujc.superkid.toolbar.ToolbarKeeper;
import cn.dujc.superkid.utils.Preferences;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * A simple {@link Fragment} subclass.`
 */
public class MatchFragment extends BaseFragment implements View.OnClickListener,MatchFragmentLeftAdapter.OnLeftItemClickListener, MatchFragmentRightAdapter.OnRightItemClickListener {
    private static final String TAG = "MatchFragment";
    @BindView(R.id.rv_left)
    RecyclerView rvLeft;
    @BindView(R.id.rv_right)
    RecyclerView rvRight;
    Unbinder unbinder;
    private View mLastSelected = null;
    private List<CupIndexResults.DataBean> data = new ArrayList<>();
    private List<CupIndexResults.DataBean.CupBean> cupBeanList = new ArrayList<>();
    private MatchFragmentLeftAdapter matchFragmentLeftAdapter;
    private MatchFragmentRightAdapter matchFragmentRightAdapter;

    @Override
    public int getViewId() {
        return R.layout.fragment_match;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {

    }
    @Nullable
    @Override
    public Toolbar initToolbar(ViewGroup parent) {
        final Toolbar toolbar = (Toolbar) LayoutInflater.from(parent.getContext()).inflate(R.layout.toolbar_fragment, parent, false);
        toolbar.findViewById(R.id.toolbar_menu_id).setOnClickListener(this);
        toolbar.findViewById(R.id.toolbar_title_id).setOnClickListener(this);
        return toolbar;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toolbar_menu_id: {
                starter().go(OnlineAnswerActivity.class);
            }
            break;
            case R.id.toolbar_title_id: {
                starter().go(SearchActivity.class);
            }
            break;
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rvLeft.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvRight.setLayoutManager(new LinearLayoutManager(getActivity()));
        matchFragmentRightAdapter = new MatchFragmentRightAdapter(cupBeanList, getActivity(), this);
        rvRight.setAdapter(matchFragmentRightAdapter);
        matchFragmentLeftAdapter = new MatchFragmentLeftAdapter(getActivity(), MatchFragment.this);
        rvLeft.setAdapter(matchFragmentLeftAdapter);
        loadData();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    private void loadData() {
        if (!Preferences.isLogin(this)) {
            return;
        }
        final RegisterResult loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getCupMatch().cupIndex(QMap.build()
                .put("user_id", loginResult.getUid())
                .put("channel", Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode", loginResult.getLogincode())
                .create()).enqueue(new Callback<CupIndexResults>() {
            @Override
            public void onResponse(Call<CupIndexResults> call, retrofit2.Response<CupIndexResults> response) {
                Log.d(TAG, "onResponse: ");
                if (cupBeanList != null) cupBeanList.clear();
                CupIndexResults body = response.body();
                if (body != null && !TextUtils.isEmpty(body.toString())) {
                    data = body.getData();
                    Log.d(TAG, "onResponse: data=" + data.toString());
                    matchFragmentLeftAdapter.setData(data);
                    List<CupIndexResults.DataBean.CupBean> cup = data.get(0).getCup();
                    addToList(cup);
                    matchFragmentRightAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<CupIndexResults> call, Throwable t) {
                Log.d(TAG, "onResponse: ");
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onLeftItemClickListener(List<CupIndexResults.DataBean.CupBean> cup) {
        if (cupBeanList != null) cupBeanList.clear();
        addToList(cup);
        matchFragmentRightAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRightItemClickListener(CupIndexResults.DataBean.CupBean.ClistBean clistBean) {
        starter()
                .with("course_id", clistBean.getId()+"")
                .with("title",clistBean.getTitle())
                .with("type",3)
                .go(MatchDetailActivity.class);
    }

    public void addToList(List<CupIndexResults.DataBean.CupBean> list ) {
        if (list != null) {
            for (CupIndexResults.DataBean.CupBean cupBean :
                    list) {
                cupBeanList.add(cupBean);
            }
        }
    }

}
